<template>
  <div>
    <div class="page-breadcrumb-wrap">
      <el-card class="page-breadcrumb">
        <span class="breadcrumb-button" @click="goBack">设计方案</span>
        <span class="breadcrumb-line">/</span>
        <span class="breadcrumb-active">方案详情</span>
      </el-card>
    </div>
    <div class="detail-box">
      <el-card>
        <el-form
          ref="form"
          :style="{ height: formHeight + 'px', 'overflow-y': 'auto', 'overflow-x': 'hidden' }"
          :model="dataForm"
          label-width="120px"
        >
          <div class="form-header">
            <span class="form-title">审批详情</span>
            <el-button type="primary" plain @click="goBack()">返 回</el-button>
          </div>
          <el-row>
            <el-col :span="8" class="mt10">
              <el-form-item label="项目:">
                {{ rowData.lineName }}
              </el-form-item>
            </el-col>
            <el-col :span="8" class="mt10">
              <el-form-item label="分部:">
                {{ rowData.lotName }}
              </el-form-item>
            </el-col>
            <el-col :span="8" class="mt10">
              <el-form-item label="工区:">
                {{ rowData.workArea }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="审批名称:">
                {{ rowData.projectName }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="权属单位:">
                {{ rowData.ownershipUnit }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="权属单位联系人:">
                {{ rowData.linkName }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="联系方式:">
                {{ rowData.linkPhone }}
              </el-form-item>
            </el-col>
          </el-row>
          <div class="form-header mt10">
            <span class="form-title">关联审批流程</span>
          </div>
          <el-row>
            <el-col :span="8" class="mt10">
              <el-form-item label="模型编码:">
                {{ rowData.moduleCode }}
              </el-form-item>
            </el-col>
            <el-col :span="8" class="mt10">
              <el-form-item label="模型名称:">
                {{ rowData.moduleName }}
              </el-form-item>
            </el-col>
            <el-col :span="8" class="mt10">
              <el-form-item label="生效版本:">
                {{ rowData.version }}
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="流程描述:">
                {{ rowData.remark }}
              </el-form-item>
            </el-col>
          </el-row>
          <div class="form-header mt10" style="border-bottom: none">
            <span class="form-title">设计方案</span>
            <span
            ><el-button
              icon="el-icon-time"
              title="历史版本"
              circle
              @click="viewHistoryVersion"
              v-if="rowData.schemeId"
            ></el-button
            ></span>
          </div>
          <div class="center-card">
            <div class="card-left">
              <span class="form-sub-title">涉敏区域</span>
              <div class="card-left-body">
                <el-col :span="24">
                  <el-form-item label="BIM模型:" label-width="120px">
                    {{ rowData.sensitiveAreaBim }}
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="GIS边界文件:" label-width="120px">
                    <div style="height: 100px; overflow: auto; overflow-x: hidden">
                      <div class="file-item" v-for="item in rowData.gisFiles" :key="item.fileId">
                        <img src="../../../../assets/icon/antFill-file-excel.png" class="file-item-icon" alt="" />
                        <div class="file-item-content">
                          <div class="file-item-text" :title="item.fileName">{{ item.fileName }}</div>
                          <div class="file-item-button">
                            <el-button
                              type="text"
                              @click="download(item)"
                            ><i class="el-icon-download"></i>下载</el-button
                            >
                            <el-button type="text" @click="filePreview(item)">预览</el-button>
                          </div>
                        </div>
                      </div>
                    </div>
                  </el-form-item>
                </el-col>
              </div>
            </div>
            <div class="card-line"></div>
            <div class="card-center">
              <span class="form-sub-title">施工设计</span>
              <div class="card-center-body">
                <el-col :span="24">
                  <el-form-item label="BIM模型:" label-width="120px">
                    {{ rowData.constructDesignBim }}
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="设计方案文件:" label-width="120px">
                    <div style="height: 100px; overflow: auto; overflow-x: hidden">
                      <div class="file-item" v-for="item in rowData.schemeFiles" :key="item.fileId">
                        <img src="../../../../assets/icon/antFill-file-excel.png" class="file-item-icon" alt="" />
                        <div class="file-item-content">
                          <div class="file-item-text" :title="item.fileName">{{ item.fileName }}</div>
                          <div class="file-item-button">
                            <el-button
                              type="text"
                              @click="download(item)"
                            ><i class="el-icon-download"></i>下载</el-button
                            >
                            <el-button type="text" @click="filePreview(item)">预览</el-button>
                          </div>
                        </div>
                      </div>
                    </div>
                  </el-form-item>
                </el-col>
              </div>
            </div>
            <div class="card-line"></div>
            <div class="card-right">
              <span class="form-sub-title">涉敏区域清单</span>
              <div class="card-right-body">
                <el-table :data="sensitiveAreaList" stripe v-loading="loading" max-height="170px" style="width: 100%">
                  <el-table-column prop="engineeringName" label="工程实体名称" show-overflow-tooltip align="center">
                  </el-table-column>
                  <el-table-column prop="mileage" label="里程范围（公里）" show-overflow-tooltip align="center">
                  </el-table-column>
                  <el-table-column prop="buildingType" label="建(构)筑物类型" show-overflow-tooltip align="center">
                  </el-table-column>
                  <el-table-column prop="throughType" label="穿越类型" show-overflow-tooltip align="center">
                  </el-table-column>
                </el-table>
              </div>
            </div>
          </div>
          <div v-if="rowData.processInstanceId">
            <div class="form-header" style="margin-top: 40px">
              <span class="form-title">审批进度</span>
              <el-button type="text" @click="viewFlowPicture">查看流程图</el-button>
            </div>
            <div style="padding: 10px">
              <el-steps :active="active" align-center>
                <el-step
                  v-for="(item, index) in flowList"
                  :title="item.taskName + ' ' + item.userName"
                  :key="index"
                  :icon="!item.endTime ? 'el-icon-info' : 'el-icon-circle-check'"
                >
                  <template #title>
                    <span>{{ item.taskName + ' ' + item.userName }}</span>
                    <span v-if="!item.endTime" class="stepStatus">待审批</span>
                  </template>
                  <template #description>
                    <div>
                      <div style="color: #303133">{{ item.endTime }}</div>
                      <el-popover placement="top" trigger="click">
                        <div class="file-item" v-for="ele in item.attachments" :key="ele.fileId">
                          <img src="../../../../assets/icon/antFill-file-excel.png" class="file-item-icon" alt="" />
                          <div class="file-item-content">
                            <div class="file-item-text" :title="ele.originalFileName">{{ ele.originalFileName }}</div>
                            <div class="file-item-button">
                              <el-button type="text" @click="download(ele)">下载</el-button>
                              <el-button type="text" @click="filePreview(ele)">预览</el-button>
                            </div>
                          </div>
                        </div>
                        <el-button slot="reference" v-if="item.attachments.length > 0" type="text">查看</el-button>
                      </el-popover>
                    </div>
                    <div
                      :title="item.comment"
                      style="color: red; width: 100%; overflow: hidden; white-space: nowrap; text-overflow: ellipsis"
                      v-if="item.comment"
                    >
                      {{ item.comment }}
                    </div>
                  </template>
                </el-step>
              </el-steps>
            </div>
          </div>
          <div v-if="rowData.status === 30">
            <div class="form-header">
              <span class="form-title">审批情况 </span>
              <el-button
                style="margin-left: 20px"
                @click="editApprovalStatus"
                v-if="menuButtonPermissions.includes('gz-metro-server.designScheme.updApprovalInfo')"
              ><i class="el-icon-edit"></i>编辑</el-button
              >
            </div>
            <el-row>
              <el-col :span="8" class="mt10">
                <el-form-item label="权属/管理单位:" label-width="200px">
                  {{ rowData.ownershipUnit }}
                </el-form-item>
              </el-col>
              <el-col :span="8" class="mt10">
                <el-form-item label="业主代表:" label-width="200px">
                  {{ rowData.ownerName }}
                </el-form-item>
              </el-col>
              <el-col :span="8" class="mt10">
                <el-form-item label="工程实体名称:" label-width="200px">
                  {{ rowData.engineeringName }}
                </el-form-item>
              </el-col>
              <el-col :span="8" class="mt10">
                <el-form-item label="建（构）筑物名称:" label-width="200px">
                  {{ rowData.buildingName }}
                </el-form-item>
              </el-col>
              <el-col :span="8" class="mt10">
                <el-form-item label="建（构）筑物类型:" label-width="200px">
                  {{ $dictTranslation(rowData.buildingType+'', buildingTypeList) }}
                </el-form-item>
              </el-col>
              <el-col :span="8" class="mt10">
                <el-form-item label="穿越类型:" label-width="200px">
                  {{ $dictTranslation(rowData.throughType+'', throughTypeList) }}
                </el-form-item>
              </el-col>
              <el-col :span="8" class="mt10">
                <el-form-item label="所在行政区:" label-width="200px">
                  {{ rowData.region }}
                </el-form-item>
              </el-col>

              <el-col :span="8" class="mt10">
                <el-form-item label="里程范围:" label-width="200px">
                  {{ rowData.mileageRange }}
                </el-form-item>
              </el-col>
              <el-col :span="8" class="mt10">
                <el-form-item label="距工程实体距离:" label-width="200px">
                  {{ rowData.engineeringDistance }}
                </el-form-item>
              </el-col>
              <el-col :span="8" class="mt10">
                <el-form-item label="地铁结构埋深:" label-width="200px">
                  {{ rowData.buriedDepth }}
                </el-form-item>
              </el-col>

              <el-col :span="8" class="mt10">
                <el-form-item label="地质情况:" label-width="200px">
                  {{ rowData.geology }}
                </el-form-item>
              </el-col>

              <el-col :span="8" class="mt10">
                <el-form-item label="设计方案审批意见状态:" label-width="200px">
                  {{ rowData.approvalStatus }}
                </el-form-item>
              </el-col>
              <el-col :span="8" class="mt10">
                <el-form-item label="设计方案审批意见时间:" label-width="200px">
                  {{ rowData.approvalTime }}
                </el-form-item>
              </el-col>
              <el-col :span="8" class="mt10">
                <el-form-item label="计划完成设计方案审批时间:" label-width="200px">
                  {{ rowData.planTime }}
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <!-- <div class="backBox">
            <el-button type="primary" @click="goBack()">返 回</el-button>
          </div> -->
        </el-form>
      </el-card>
    </div>
    <flow-picture ref="flowPictureRef" />
    <history-version ref="historyVersionRef" />
    <approval-status ref="approvalStatusRef" @goBack="goBack" />
  </div>
</template>
<script>
import actions from '@/cros/actions'
import { download, getDetail, getModuleById, getProcessHistory, getSensitiveArea } from '@/api/designingScheme'
import FlowPicture from '@/views/designingScheme/designingScheme/components/FlowPicture.vue'
import HistoryVersion from '@/views/designingScheme/designingScheme/components/HistoryVersion.vue'
import ApprovalStatus from '@/views/designingScheme/designingScheme/components/ApprovalStatus.vue'
import { dictDataList } from '@/api/approvalSetting'

export default {
  name: 'DetailPage',
  components: { ApprovalStatus, HistoryVersion, FlowPicture },
  data () {
    return {
      dataForm: {},
      rowData: {},
      active: 0,
      flowList: [],
      menuButtonPermissions: [],
      formHeight: 500,
      pageNo: 1,
      totalPage: 0,
      sensitiveAreaList: [],
      loading: false,
      throughTypeList: [],
      buildingTypeList: []
    }
  },
  mounted () {
    dictDataList({
      dictName: '',
      dictType: 'through_type',
      dictValue: '',
      pageNo: 1,
      pageSize: 9999,
      searchType: 'project'
    }).then(res => {
      this.throughTypeList = res.data.list
    })
    dictDataList({
      dictName: '',
      dictType: 'building_type',
      dictValue: '',
      pageNo: 1,
      pageSize: 9999,
      searchType: 'project'
    }).then(res => {
      this.buildingTypeList = res.data.list
    })
    actions.onGlobalStateChange((state, prevState) => {
      this.menuButtonPermissions = state.menuButtonPermissions.map((v) => v.buttonKey)
    }, true)
    // 监听窗口大小变化事件
    window.addEventListener('resize', this.setTableHeight)
    // 初始化表格高度
    this.setTableHeight()
    // 获取表格对象
    const dom = document.querySelector('.el-table__body-wrapper')
    dom.addEventListener('scroll', (v) => {
      const scrollDistance = dom.scrollHeight - dom.scrollTop - dom.clientHeight
      // 判断是否到底，可以加载下一页
      if (scrollDistance <= 1) {
        // 判断是否全部加载完成
        if (this.pageNo >= this.totalPage) return
        if (this.pageNo < this.totalPage) {
          // 当前页数小于总页数就请求
          this.pageNo++ // 当前页数自增
          // 加载下一页方法
          this.getNextList()
        }
      }
    })
  },
  methods: {
    filePreview (item) {
      this.$filePreview(item)
    },
    async getNextList () {
      this.loading = true
      const res = await getSensitiveArea({
        schemeId: this.rowData.schemeId,
        batchNo: this.rowData.batchNo,
        pageNo: this.pageNo,
        pageSize: 10
      })
      this.sensitiveAreaList = [...this.sensitiveAreaList, ...res.data.list]
      this.totalPage = res.data.totalCount ? Math.ceil(res.data.totalCount / 10) : 0
      this.loading = false
    },
    setTableHeight () {
      this.$nextTick(() => {
        this.formHeight = document.body.offsetHeight - 160
      })
    },
    init (row) {
      console.log(row)
      this.rowData = JSON.parse(JSON.stringify(row))
      this.sensitiveAreaList = []
      if (row.schemeId) {
        getDetail({ schemeId: row.schemeId }).then((res) => {
          console.log(res)
          if (res.code === 'success' && res.data) {
            this.$set(this.rowData, 'moduleId', res.data.moduleId)
            this.$set(this.rowData, 'sensitiveAreaBim', res.data.sensitiveAreaBim)
            this.$set(this.rowData, 'gisFiles', res.data.gisFiles)
            this.$set(this.rowData, 'constructDesignBim', res.data.constructDesignBim)
            this.$set(this.rowData, 'preFinishTime', res.data.preFinishTime)
            this.$set(this.rowData, 'schemeFiles', res.data.schemeFiles)
            this.$set(this.rowData, 'moduleId', res.data.moduleId)
            this.$set(this.rowData, 'versionId', res.data.versionId)
            this.$set(this.rowData, 'processInstanceId', res.data.processInstanceId)
            this.$set(this.rowData, 'status', res.data.status)
            this.$set(this.rowData, 'batchNo', res.data.batchNo)
              this.$set(this.rowData, 'ownershipUnit', res.data.ownershipUnit)
              this.$set(this.rowData, 'ownerName', res.data.ownerName)
              this.$set(this.rowData, 'engineeringName', res.data.engineeringName)
              this.$set(this.rowData, 'buildingName', res.data.buildingName)
              this.$set(this.rowData, 'buildingType', res.data.buildingType)
              this.$set(this.rowData, 'mileageRange', res.data.mileageRange)
              this.$set(this.rowData, 'engineeringDistance', res.data.engineeringDistance)
              this.$set(this.rowData, 'buriedDepth', res.data.buriedDepth)
              this.$set(this.rowData, 'throughType', res.data.throughType)
              this.$set(this.rowData, 'geology', res.data.geology)
              this.$set(this.rowData, 'region', res.data.region)
              this.$set(this.rowData, 'approvalStatus', res.data.approvalStatus)
              this.$set(this.rowData, 'approvalTime', res.data.approvalTime)
              this.$set(this.rowData, 'planTime', res.data.planTime)
            this.getNextList()
            getModuleById({
              modelId: this.rowData.moduleId,
              versionId: this.rowData.versionId
            }).then((res) => {
              if (res.code === 'success') {
                this.$set(this.rowData, 'moduleCode', res.data.moduleCode)
                this.$set(this.rowData, 'moduleName', res.data.moduleName)
                this.$set(this.rowData, 'version', res.data.version)
                this.$set(this.rowData, 'remark', res.data.remark)
              }
            })
            if (this.rowData.processInstanceId) {
              this.getProcessHistory()
            }
          }
        })
      } else {
        getModuleById({
          modelId: this.rowData.moduleId,
          versionId: this.rowData.versionId
        }).then((res) => {
          if (res.code === 'success') {
            this.$set(this.rowData, 'moduleCode', res.data.moduleCode)
            this.$set(this.rowData, 'moduleName', res.data.moduleName)
            this.$set(this.rowData, 'version', res.data.version)
            this.$set(this.rowData, 'remark', res.data.remark)
          }
        })
      }
    },
    // 获取流程进度
    getProcessHistory () {
      getProcessHistory({ processId: this.rowData.processInstanceId }).then((res) => {
        if (res.code === 'success') {
          this.flowList = res.data
          this.flowList.forEach((item, index) => {
            if (item.endTime) {
              this.active = index + 1
            }
          })
          // if (this.flowList[this.flowList.length - 1].endTime) {
          //   this.active = this.flowList.length
          // }
        }
      })
    },
    // 查看历史版本
    viewHistoryVersion () {
      this.$nextTick(() => {
        this.$refs.historyVersionRef.init(this.rowData.schemeId)
      })
    },
    // 下载文件
    download (row) {
      download(row.fileId).then((res) => {
        console.log(res)
        const blob = new Blob([res.res])
        const url = window.URL.createObjectURL(blob)
        const link = document.createElement('a') // 创建a标签
        link.href = url
        link.download = row.originalFileName || row.fileName // 重命名文件
        link.click()
        URL.revokeObjectURL(url) // 释放内
      })
    },
    // 查看流程图
    viewFlowPicture () {
      this.$nextTick(() => {
        this.$refs.flowPictureRef.init(this.rowData.processInstanceId)
      })
    },
    // 编辑审批情况
    editApprovalStatus () {
      this.$nextTick(() => {
        this.$refs.approvalStatusRef.init(this.rowData)
      })
    },
    goBack () {
      this.$emit('changeShowType', 1)
    }
  }
}
</script>
<style scoped lang="scss">
.mt10 {
  margin-top: 10px;
}

.detail-box {
  padding: 0 10px;
}

.form-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px;
  border-bottom: 1px solid #ddd;
}

.form-title {
  color: #101010;
  font-size: 16px;
  font-weight: bold;
}

.form-sub-title {
  margin-bottom: 10px;
  color: #101010;
  font-size: 15px;
  font-weight: bold;
}

.center-card {
  display: flex;
  align-items: center;
  width: 100%;
  padding: 20px;
  border-radius: 4px;
  border: 1px solid #ebeef5;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.card-left {
  width: calc(33% - 21px);

  .card-left-body {
    padding: 10px 30px;
    height: 200px;
  }
}

.card-center {
  width: calc(33% - 21px);

  .card-center-body {
    padding: 10px 30px;
    height: 200px;
  }
}

.card-right {
  width: calc(33% - 21px);

  .card-right-body {
    padding: 10px 0;
    height: 200px;
  }
}

.card-line {
  margin: 0 20px;
  height: 180px;
  width: 1px;
  background: #ddd;
}

.file-item {
  display: flex;
  margin-bottom: 10px;
}

.file-item-icon {
  width: 45px;
  height: 50px;
}

.file-item-content {
  margin-left: 10px;
  height: 50px;
  width: calc(100% - 45px);
  .file-item-text {
    height: 25px;
    font-weight: 700;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }

  .file-item-button {
    height: 25px;
  }
}

::v-deep .el-step__title.is-finish {
  font-weight: 700;
  color: #303133;
}
::v-deep .el-step__title.is-wait {
  font-weight: 700;
  color: #303133;
}
::v-deep .el-step__head.is-success {
  color: #409eff;
  border-color: #409eff;
}
::v-deep .el-icon-info {
  color: #eca600;
}
::v-deep .el-step__title {
  font-size: 14px;
}
::v-deep .el-step {
  margin-top: 20px;
  flex-basis: 20% !important;
  max-width: none !important;
  width: 20% !important;
}
::v-deep .el-steps {
  flex-wrap: wrap;
}
.backBox {
  margin: 20px 0;
  text-align: center;
}
.stepStatus {
  color: #eca600;
  border: 1px solid #eca600;
  padding: 2px;
  border-radius: 4px;
  margin-left: 10px;
  font-size: 12px;
}
</style>
